﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reputation.Tests.ServiceabilityTest.Helper;
using Microsoft.Reputation.Tests.FrontEndCommon;


namespace SendRequest
{
    public partial class MainFrm : Form
    {
        MrsTelemetryServiceProvider tsProvider;
        MrsDeliveryServiceProvider dsProvider;

        int requestNum = 1;
 
        public MainFrm()
        {
            InitializeComponent();
            dsLoopComBox.SelectedIndex = 1;
            tsLoopCombox.SelectedIndex = 1;
        }         
       

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutFrm aboutFrmInstance = new AboutFrm();
            aboutFrmInstance.Show();
        }

        private void SendUrlUsageBtn_Click(object sender, EventArgs e)
        {
            finishLabel.Visible = false;

            try
            {
                string webServiceUrl = TelemetryUrlTextBox.Text;
                tsProvider = new MrsTelemetryServiceProvider(webServiceUrl);
                int loopNum = int.Parse(tsLoopCombox.Text);
                 
                uint result = 0;

                for (int i = 0; i < requestNum; i++)
                {
                    result = tsProvider.SendUrlUsageReports();
                    if (result != 0)
                    {
                        MessageBox.Show("Error code: " + result.ToString());
                        return;
                    }
                    else
                    {
                        SendUrlUsageBtn.BackColor = Color.Azure;
                        SendUrlUsageBtn.Text = string.Format("Finished: {0}", i+1);
                    }
                } 
                
                finishLabel.Visible = true;
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void SendErrortReportBtn_Click(object sender, EventArgs e)
        {
            finishLabel.Visible = false;

            try
            {
                string webServiceUrl = TelemetryUrlTextBox.Text;
                tsProvider = new MrsTelemetryServiceProvider(webServiceUrl);
                int loopNum = int.Parse(tsLoopCombox.Text);

                uint result = 0;

                for (int i = 0; i < requestNum; i++)
                {
                    result = tsProvider.SendUrlErrorReports();
                    if (result != 0)
                    {
                        MessageBox.Show("Error code: " + result.ToString());
                        return;
                    }
                    else
                    {
                        SendErrortReportBtn.BackColor = Color.Azure;
                        SendErrortReportBtn.Text = string.Format("Finished: {0}", i + 1);
                    }
                } 
                
                finishLabel.Visible = true;
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void SendLookupBtn_Click(object sender, EventArgs e)
        {
            finishLabel.Visible = false;

            string webServiceUrl = DeliveryUrlTextBox.Text;
            dsProvider = new MrsDeliveryServiceProvider(Guid.NewGuid(), "test", Guid.NewGuid(), Guid.NewGuid(), webServiceUrl);

            IList<MrsLookupRequest> requests = new List<MrsLookupRequest>();
            IList<MrsLookupResponse> lookupResults;
            DataConvertor dataConvert = new DataConvertor();
            int urlCount = 1;
            List<string> randUrlObjs = MrsDeliveryServiceHelper.GetRandomUrls(urlCount);
            foreach (string url in randUrlObjs)
            {
                requests.Add(MrsDeliveryServiceHelper.BuildDataBlockRequest(url));
            }

            MrsFilter filter = new MrsFilter(null, null, null, null);

            try
            {
                for (int i = 0; i < requestNum; i++)
                {
                    dsProvider.LookupObjectAPI(requests, filter, out lookupResults);
                    (sender as Button).Text = string.Format("Finished: {0}", i + 1);
                }

                finishLabel.Visible = true;
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                for (int i = 0; i < requestNum; i++)
                {
                    dsProvider.LookupObjectAPI(requests, filter, out lookupResults);
                    (sender as Button).Text = string.Format("Finished: {0}", i + 1);
                }
            }
        }

        private void SendLookupUrlBtn_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtboxURL.Text))
            {
                MessageBox.Show("please input URL");
                return;
            }

            finishLabel.Visible = false;

            try
            {
                string webServiceUrl = DeliveryUrlTextBox.Text;
                Guid gd = new Guid("10f54ef1-f5bd-4fd3-b9c7-15eb086133bb");
                dsProvider = new MrsDeliveryServiceProvider(gd, "test", Guid.NewGuid(), Guid.NewGuid(), webServiceUrl);
                
                
                Microsoft.Reputation.Services.CategoryReputation[] categories = new Microsoft.Reputation.Services.CategoryReputation[20];
               

                DateTime beginTime = DateTime.Now;
                for (int i = 0; i < requestNum; i++)
                {
                    categories = dsProvider.LookupCategoriesForUrlAPI(txtboxURL.Text);
                    (sender as Button).Text = string.Format("Finished: {0}", i + 1);                               
                }

                DateTime endTime = DateTime.Now;
                TimeSpan diffTime = endTime.Subtract(beginTime);
                float expectValue = (float)(diffTime.TotalSeconds / requestNum);

                finishLabel.Text = "Finished in " + expectValue + ". Status: " + dsProvider.OperationStatus;
                finishLabel.Visible = true;

                string categoryStr = string.Empty;                

                foreach (Microsoft.Reputation.Services.CategoryReputation category in categories)
                {
                    categoryStr += "Categary ID: " + category.CategoryId.ToString()
                                 + " Confidence: " + category.Confidence.ToString()
                                 + '\r';
                }

                lblCategories.Text = categoryStr;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dsLoopComBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            requestNum = int.Parse(dsLoopComBox.SelectedItem.ToString());
        }

        private void tsLoopCombox_SelectedIndexChanged(object sender, EventArgs e)
        {
            requestNum = int.Parse(tsLoopCombox.SelectedItem.ToString());
        }

    }
}
